using System;
using System.ComponentModel;
using System.Drawing;
using System.Globalization;
using System.Security.Permissions;
using System.Web;
using System.Web.UI;
using System.Windows.Forms;
using PickGold.Charting.Utilities;

namespace PickGold.Charting
{
	[DefaultProperty("Data"), SRDescription("DescriptionAttributeElementPosition_ElementPosition"), AspNetHostingPermission(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal), AspNetHostingPermission(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
	public class ElementPosition : ChartElement
	{
		internal bool _auto;
		private float _height;
		private float _width;
		private float _x;
		private float _y;
		internal bool resetAreaAutoPosition;

		public ElementPosition()
		{
			this._auto = true;
		}

		internal ElementPosition(IChartElement parent)
			: base(parent)
		{
			this._auto = true;
		}

		public ElementPosition(float x, float y, float width, float height)
		{
			this._auto = true;
			this._auto = false;
			this._x = x;
			this._y = y;
			this._width = width;
			this._height = height;
		}

		internal override bool EqualsInternal(object obj)
		{
			ElementPosition position = obj as ElementPosition;
			if (position != null)
			{
				if (this._auto && (this._auto == position._auto))
				{
					return true;
				}
				if (((this._x == position._x) && (this._y == position._y)) && ((this._width == position._width) && (this._height == position._height)))
				{
					return true;
				}
			}
			return false;
		}

		public void FromRectangleF(RectangleF rect)
		{
			this._x = rect.X;
			this._y = rect.Y;
			this._width = rect.Width;
			this._height = rect.Height;
			this._auto = false;
		}

		private void ResetAllAreasAutoPosition(bool autoValue)
		{
			if (((this.resetAreaAutoPosition && (base.Chart != null)) && (base.Chart.IsDesignMode() && !base.Chart.Serializing)) && (base.Chart.ChartAreas.Count > 1))
			{
				bool auto = base.Chart.ChartAreas[0].Position.Auto;
				bool flag2 = true;
				foreach (ChartArea area in base.Chart.ChartAreas)
				{
					if (area.Position.Auto != auto)
					{
						flag2 = false;
						break;
					}
				}
				if (flag2)
				{
					string messageChangingChartAreaPositionProperty = SR.MessageChangingChartAreaPositionProperty;
					if (autoValue)
					{
						messageChangingChartAreaPositionProperty = messageChangingChartAreaPositionProperty + SR.MessageChangingChartAreaPositionConfirmAutomatic;
					}
					else
					{
						messageChangingChartAreaPositionProperty = messageChangingChartAreaPositionProperty + SR.MessageChangingChartAreaPositionConfirmCustom;
					}
					if (MessageBox.Show(messageChangingChartAreaPositionProperty, SR.MessageChartTitle, MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1, GlobalizationHelper.GetMessageBoxOptions()) == DialogResult.Yes)
					{
						foreach (ChartArea area2 in base.Chart.ChartAreas)
						{
							if (autoValue)
							{
								this.SetPositionNoAuto(0f, 0f, 0f, 0f);
							}
							area2.Position._auto = autoValue;
						}
					}
				}
			}
		}

		internal void SetPositionNoAuto(float x, float y, float width, float height)
		{
			bool flag = this._auto;
			this._x = x;
			this._y = y;
			this._width = width;
			this._height = height;
			this._auto = flag;
		}

		public RectangleF ToRectangleF()
		{
			return new RectangleF(this._x, this._y, this._width, this._height);
		}

		internal override string ToStringInternal()
		{
			string str = "Auto";
			if (!this._auto)
			{
				str = this._x.ToString(CultureInfo.CurrentCulture) + ", " + this._y.ToString(CultureInfo.CurrentCulture) + ", " + this._width.ToString(CultureInfo.CurrentCulture) + ", " + this._height.ToString(CultureInfo.CurrentCulture);
			}
			return str;
		}

		[SRDescription("DescriptionAttributeElementPosition_Auto"), NotifyParentProperty(true), RefreshProperties(RefreshProperties.All), PersistenceMode(PersistenceMode.Attribute), Bindable(true), DefaultValue(true), SRCategory("CategoryAttributeMisc")]
		public bool Auto
		{
			get
			{
				return this._auto;
			}
			set
			{
				if (value != this._auto)
				{
					this.ResetAllAreasAutoPosition(value);
					if (value)
					{
						this._x = 0f;
						this._y = 0f;
						this._width = 0f;
						this._height = 0f;
					}
					this._auto = value;
					this.Invalidate();
				}
			}
		}

		[Browsable(false), SerializationVisibility(SerializationVisibility.Hidden)]
		public float Bottom
		{
			get
			{
				return (this._y + this._height);
			}
		}

		[PersistenceMode(PersistenceMode.Attribute), SRCategory("CategoryAttributeMisc"), RefreshProperties(RefreshProperties.All), Bindable(true), NotifyParentProperty(true), SRDescription("DescriptionAttributeElementPosition_Height"), DefaultValue((float)0f)]
		public float Height
		{
			get
			{
				return this._height;
			}
			set
			{
				if ((value < 0.0) || (value > 100.0))
				{
					throw new ArgumentOutOfRangeException("value", SR.ExceptionElementPositionArgumentOutOfRange);
				}
				this._height = value;
				this.Auto = false;
				if ((this._y + this.Height) > 100f)
				{
					this._y = 100f - this.Height;
				}
				this.Invalidate();
			}
		}

		[Browsable(false), SerializationVisibility(SerializationVisibility.Hidden)]
		public float Right
		{
			get
			{
				return (this._x + this._width);
			}
		}

		[SerializationVisibility(SerializationVisibility.Hidden), Browsable(false)]
		public SizeF Size
		{
			get
			{
				return new SizeF(this._width, this._height);
			}
		}

		[NotifyParentProperty(true), DefaultValue((float)0f), SRDescription("DescriptionAttributeElementPosition_Width"), RefreshProperties(RefreshProperties.All), PersistenceMode(PersistenceMode.Attribute), SRCategory("CategoryAttributeMisc"), Bindable(true)]
		public float Width
		{
			get
			{
				return this._width;
			}
			set
			{
				if ((value < 0.0) || (value > 100.0))
				{
					throw new ArgumentOutOfRangeException("value", SR.ExceptionElementPositionArgumentOutOfRange);
				}
				this._width = value;
				this.Auto = false;
				if ((this._x + this.Width) > 100f)
				{
					this._x = 100f - this.Width;
				}
				this.Invalidate();
			}
		}

		[SRDescription("DescriptionAttributeElementPosition_X"), Bindable(true), PersistenceMode(PersistenceMode.Attribute), SRCategory("CategoryAttributeMisc"), DefaultValue((float)0f), NotifyParentProperty(true), RefreshProperties(RefreshProperties.All)]
		public float X
		{
			get
			{
				return this._x;
			}
			set
			{
				if ((value < 0.0) || (value > 100.0))
				{
					throw new ArgumentOutOfRangeException("value", SR.ExceptionElementPositionArgumentOutOfRange);
				}
				this._x = value;
				this.Auto = false;
				if ((this._x + this.Width) > 100f)
				{
					this.Width = 100f - this._x;
				}
				this.Invalidate();
			}
		}

		[SRCategory("CategoryAttributeMisc"), PersistenceMode(PersistenceMode.Attribute), Bindable(true), DefaultValue((float)0f), SRDescription("DescriptionAttributeElementPosition_Y"), NotifyParentProperty(true), RefreshProperties(RefreshProperties.All)]
		public float Y
		{
			get
			{
				return this._y;
			}
			set
			{
				if ((value < 0.0) || (value > 100.0))
				{
					throw new ArgumentOutOfRangeException("value", SR.ExceptionElementPositionArgumentOutOfRange);
				}
				this._y = value;
				this.Auto = false;
				if ((this._y + this.Height) > 100f)
				{
					this.Height = 100f - this._y;
				}
				this.Invalidate();
			}
		}
	}
}

